package com.wzh.mall.cart.service.impl;

import com.wzh.mall.cart.interceptor.CartInterceptor;
import com.wzh.mall.cart.service.CartService;
import com.wzh.mall.cart.vo.CartItem;
import com.wzh.mall.cart.vo.UserInfoTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @program: mall
 * @description:
 * @author: Mr.Wang
 * @create: 2021-09-17 17:24
 **/
@Slf4j
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    StringRedisTemplate redisTemplate;

    // redis中购物车的前缀
    private final String CART_PREFIX="mall:cart:";

    @Override
    public CartItem addToCart(Long skuId, Integer num) {
        BoundHashOperations<String, Object, Object> cartOps = getCartOps();
        // 1、远程查询当前需要添加的商品的信息


        return null;
    }

    /**
      * @Description: 获取到我们需要操作的购物车
      * @Param: []
      * @return: org.springframework.data.redis.core.BoundHashOperations<java.lang.String,java.lang.Object,java.lang.Object>
      * @Author: Mr.Wang
      * @Date: 2021/10/9
      * @Time: 18:34
      */      
      
    private  BoundHashOperations<String, Object, Object> getCartOps() {
        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();


        String cartKey="";
        if (userInfoTo.getUserId()!=null){
            // 说明用户登陆了
            // mall:cart:1
            cartKey=CART_PREFIX+userInfoTo.getUserId();
        }else {
            // 说明是临时购物车
            cartKey=CART_PREFIX+userInfoTo.getUserKey();
        }
        // 1、
        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(cartKey);

        return operations;
    }
}
